﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.ViewInfo;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;
using QLPhanQuyen.BO;

namespace QLPhanQuyen.GUI
{
    public partial class PhanQuyenForm : Office2007Form
    {
        NhomChucNangBO NCNBO = new NhomChucNangBO();
        ChucNangBO CNBO = new ChucNangBO();
        ChucNangConBO CNCBO = new ChucNangConBO();

        public PhanQuyenForm()
        {
            InitializeComponent();
        }

        private void PhanQuyenForm_Load(object sender, EventArgs e)
        {
            initTreeVaiTroVaNguoiDung();
        }

        private void btnThemNguoiDung_Click(object sender, EventArgs e)
        {
            ThemNguoiDungForm frmtnd = new ThemNguoiDungForm();
            frmtnd.ShowDialog();
        }

        private void btnThemVaiTro_Click(object sender, EventArgs e)
        {
            ThemVaiTroForm frmtvt = new ThemVaiTroForm();
            frmtvt.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {

            this.Close();
            this.Dispose();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            CapNhatNguoiDungForm frmcnnd = new CapNhatNguoiDungForm();
            frmcnnd.Show();
        }

        private List<string> listNCN()
        {
            return NCNBO.DSTenNhomChucNang();
        }

        private List<string> listCN(string maNCN)
        {
            return CNBO.DSTenChucNangTheoNhom(maNCN);
        }

        private List<string> listCNC(string maCN)
        {
            return CNCBO.DSChucNangConTheoChucNang(maCN);
        }

        #region tree Vai trò và người dùng

        private void initTreeVaiTroVaNguoiDung()
        {
            TreeListNode parentNode;
            TreeListNode childNode;
            TreeListNode grandChildNode;

            int parentIndex;
            int childIndex;
            int grandChildIndex;

            for (parentIndex = 0; parentIndex < listNCN().Count(); parentIndex++)
            {
                parentNode = tlVaiTroVaNguoiDung.AppendNode(new object[] { listNCN()[parentIndex] }, null);

                for (childIndex = 0; childIndex < listCN(NCNBO.LayMaTheoTen(listNCN()[parentIndex])).Count(); childIndex ++)
                {
                    childNode = tlVaiTroVaNguoiDung.AppendNode(
                        new object[] { listCN(NCNBO.LayMaTheoTen(listNCN()[parentIndex]))[childIndex] }, parentNode);

                    for (grandChildIndex = 0; 
                        grandChildIndex < listCNC(CNBO.LayMaTheoTen(listCN(NCNBO.LayMaTheoTen(listNCN()[parentIndex]))[childIndex])).Count(); 
                        grandChildIndex++)
                    {
                        grandChildNode = tlVaiTroVaNguoiDung.AppendNode(
                            new object[] { listCNC(CNBO.LayMaTheoTen(listCN(NCNBO.LayMaTheoTen(listNCN()[parentIndex]))[childIndex]))[grandChildIndex] }, childNode);
                    }
                }
            }
        }

        #endregion
    }
}
